package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.ElderPharmacy;
import com.yunfeisoft.business.service.inter.ElderPharmacyService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.*;
import org.apache.catalina.util.RequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * ClassName: ElderPharmacyController
 * Description: 主要用药情况从表Controller
 * Author: Jackie liu
 * Date: 2020-04-04
 */
@Controller
public class ElderPharmacyController extends BaseController {

    @Autowired
    private ElderPharmacyService elderPharmacyService;

    /**
     * 添加主要用药情况从表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderPharmacy/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(ElderPharmacy record, HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        // 获取healthId
        String[] ids = map.get("elderHealthId");
        String elderHealthId = ids[0];
        // 获取medicinalName
        String[] names = map.get("medicinalName");
        // 获取用法
        String[] uses = map.get("medicinalUse");
        // 获取用量
        String[] nums = map.get("medicinalNumber");
        // 获取用药时间
        String[] times = map.get("medicinalTime");
        // 获取依从性
        String[] medicinalCompliances = map.get("medicinalCompliance");
        List<ElderPharmacy> elderPharmacyList = new ArrayList<>();
        for (int i = 0; i < names.length; i++) {
            if (StringUtils.isBlank(names[i]) && StringUtils.isBlank(uses[i])
                    && StringUtils.isBlank(nums[i]) && StringUtils.isBlank(times[i]) && StringUtils.isBlank(medicinalCompliances[i])) {
                continue;
            }
            ElderPharmacy elderPharmacy = new ElderPharmacy();
            elderPharmacy.setElderHealthId(elderHealthId);
            elderPharmacy.setMedicinalName(names[i]);
            elderPharmacy.setMedicinalNumber(nums[i]);
            elderPharmacy.setMedicinalUse(uses[i]);
            elderPharmacy.setMedicinalCompliance(medicinalCompliances[i]);
            elderPharmacy.setMedicinalTime(DateUtils.stringToDate(times[i]));
            elderPharmacyList.add(elderPharmacy);
        }
        elderPharmacyService.saveElderPharmacy(elderPharmacyList);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改主要用药情况从表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderPharmacy/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(ElderPharmacy record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        elderPharmacyService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询主要用药情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderPharmacy/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String elderHealthId = ServletRequestUtils.getStringParameter(request, "elderHealthId", null);
        if (StringUtils.isBlank(elderHealthId)) {
            return ResponseUtils.warn("参数错误");
        }
        List<ElderPharmacy> record = elderPharmacyService.getByHealthId(elderHealthId);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询主要用药情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderPharmacy/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<ElderPharmacy> page = elderPharmacyService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除主要用药情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderPharmacy/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        elderPharmacyService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
